<template>
  <div class="cesium-draw">
    <div id="cesium-drawer" />
    <div class="mouse-postion">{{ nowPostion.wgs84 }}</div>
    <div class="tool-box">
      <!-- <div class="slider-box-wrapper"><slider-box v-model:value="terrainExaggeration" :max="2" /></div> -->
      <!-- <div class="slider-box-wrapper"><slider-box v-model:value="relativeHeight" :max="10000" /></div> -->
      <div class="select-box-wrapper"><select-box :options="locationToolbarOptions" /></div>
      <div class="select-box-wrapper"><select-box :options="entityToolbarOptions" /></div>
      <div class="select-box-wrapper"><select-box :options="sizeOptions" /></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="shadows">shadows</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="softShadows">soft shadows</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="terrainShadows">terrain shadows</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="entityShadows">entity shadows</a-checkbox></div>
      <div id="menu">
        <p><button @click="onClick('play')">播放</button></p>
        <p><button @click="onClick('stop')">暂停</button></p>
      </div>
    </div>
  </div>
</template>

<script>
//参考：Cesium深入浅出之图层管理器 https://blog.csdn.net/fywindmoon/article/details/113747843
import { DownOutlined, EditOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref, watch } from 'vue';
import SliderBox from '@components/widgets/slider-box';
import SelectBox from '@components/widgets/select-box';
import * as Cesium from '@cesium/Source/Cesium.js';
import MousePosition from '@functional/utils/mousePosition';

export default defineComponent({
  name: 'pipeline', // 地下管线
  components: { DownOutlined, EditOutlined, SliderBox, SelectBox },
  setup() {
    return {
      viewer: null,
      setEntityShadows: () => {}
    };
  },
  data() {
    return {
      nowPostion: {},
      shadows: true,
      softShadows: true,
      terrainShadows: true,
      entityShadows: true,
      locationToolbarOptions: [],
      entityToolbarOptions: [],
      sizeOptions: []
    };
  },
  computed: {},
  watch: {
    shadows(val) {
      this.viewer && (this.viewer.shadows = val);
    },
    softShadows(val) {
      this.viewer && (this.viewer.shadowMap.softShadows = val);
    },
    terrainShadows(val) {
      const terrainShadows = val ? Cesium.ShadowMode.ENABLED : Cesium.ShadowMode.DISABLED;
      this.viewer && (this.viewer.terrainShadows = terrainShadows);
    },
    entityShadows(val) {
      this.setEntityShadows(val);
    }
  },
  mounted() {
    this.initViewer();
  },
  beforeUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  },
  methods: {
    initEffects() {
      const viewer = this.viewer;

      var scene = viewer.scene;
      var globe = scene.globe;

      scene.screenSpaceCameraController.enableCollisionDetection = false;
      globe.translucency.frontFaceAlphaByDistance = new Cesium.NearFarScalar(400.0, 0.0, 800.0, 1.0);

      var longitude = -3.82518;
      var latitude = 53.11728;
      var height = 72.8;
      var position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height);
      // var url = '../SampleData/models/ParcLeadMine/ParcLeadMine.glb';
      var url = '//zzgis.com/cdn/3d/SampleData/models/ParcLeadMine/ParcLeadMine.glb';

      var entity = viewer.entities.add({
        name: url,
        position: position,
        model: {
          uri: url
        }
      });

      var polygon = viewer.entities.add({
        polygon: {
          hierarchy: new Cesium.PolygonHierarchy(
            Cesium.Cartesian3.fromDegreesArrayHeights([
              -3.8152789692233817,
              53.124521420389996,
              200.20779492422255,
              -3.8165955002619016,
              53.12555934545405,
              205.85834336951655,
              -3.8201599842222054,
              53.12388420656903,
              230.82362697069453,
              -3.8198667503545027,
              53.123748567587455,
              225.53297006293968,
              -3.8190548496317476,
              53.1240486000822,
              221.82677773619432,
              -3.817536387097508,
              53.122763476393764,
              209.94136782255705,
              -3.8169125359199336,
              53.12285547981627,
              210.96626238861327,
              -3.8166873871853073,
              53.12299403424474,
              211.02223937734595,
              -3.8163695374580873,
              53.12300505277307,
              211.25942926271824,
              -3.8162743040622313,
              53.12281471203994,
              212.35109129094147,
              -3.8159746138174193,
              53.12280996651767,
              214.87977416348798,
              -3.815429896849304,
              53.1236135347983,
              209.72496223706005
            ])
          ),
          material: Cesium.Color.LIME.withAlpha(0.5),
          classificationType: Cesium.ClassificationType.TERRAIN
        }
      });

      var polyline = viewer.entities.add({
        polyline: {
          positions: Cesium.Cartesian3.fromDegreesArrayHeights([
            -3.8098444201746373,
            53.1190304262546,
            286.1875170545701,
            -3.8099801237370663,
            53.119539531697576,
            288.7733884242394,
            -3.810165716635671,
            53.11979180761567,
            290.9294630315179,
            -3.8104840812145357,
            53.12007534956926,
            292.6392327626228,
            -3.8105689502073554,
            53.120259094792196,
            292.222036965774,
            -3.811027311824268,
            53.120409248874196,
            289.61356291617307,
            -3.811530473295422,
            53.12063281057782,
            284.01098712543586,
            -3.8120545342562693,
            53.120742539082435,
            280.118191867836,
            -3.812444493044727,
            53.120813289759326,
            276.0400221387852,
            -3.812779626711285,
            53.12094275348024,
            271.1187399484896,
            -3.8133560322579494,
            53.12104757866638,
            263.3495497598578,
            -3.8137266493960085,
            53.12120789867194,
            257.73878624321316,
            -3.8142552291751133,
            53.121321248522904,
            251.87265828778177,
            -3.814322603988525,
            53.12174170121103,
            238.7082749547689,
            -3.8143764268391314,
            53.1219492923309,
            235.0371831845662,
            -3.8148156514145786,
            53.12210819668669,
            230.2458816627467,
            -3.8155394721966163,
            53.1222990144029,
            221.33319292262706,
            -3.8159828072920927,
            53.12203093429715,
            223.66664756982703,
            -3.816678108944717,
            53.12183939425214,
            223.8787312412801,
            -3.817466081093726,
            53.121751900508535,
            224.52293229989735,
            -3.8183082996527955,
            53.12173266141031,
            223.3672181535749
          ]),
          width: 8,
          material: new Cesium.PolylineOutlineMaterialProperty({
            color: Cesium.Color.YELLOW,
            outlineWidth: 2,
            outlineColor: Cesium.Color.BLACK
          }),
          clampToGround: true
        }
      });

      var viewModel = {
        translucencyEnabled: true,
        fadeByDistance: true,
        showVectorData: false,
        alpha: 0.5
      };

      // Cesium.knockout.track(viewModel);
      // var toolbar = document.getElementById('toolbar');
      // Cesium.knockout.applyBindings(viewModel, toolbar);
      // for (var name in viewModel) {
      //   if (viewModel.hasOwnProperty(name)) {
      //     Cesium.knockout.getObservable(viewModel, name).subscribe(update);
      //   }
      // }

      function update() {
        globe.translucency.enabled = viewModel.translucencyEnabled;

        var alpha = Number(viewModel.alpha);
        alpha = !isNaN(alpha) ? alpha : 1.0;
        alpha = Cesium.Math.clamp(alpha, 0.0, 1.0);

        globe.translucency.frontFaceAlphaByDistance.nearValue = alpha;
        globe.translucency.frontFaceAlphaByDistance.farValue = viewModel.fadeByDistance ? 1.0 : alpha;

        polygon.show = viewModel.showVectorData;
        polyline.show = viewModel.showVectorData;
      }
      // update();

      viewer.scene.camera.setView({
        destination: new Cesium.Cartesian3(3826465.9884728403, -254831.02751468265, 5081387.671561018),
        orientation: new Cesium.HeadingPitchRoll(3.3889450556243754, -0.5276382514771969, 6.282272566663295),
        endTransform: Cesium.Matrix4.IDENTITY
      });

      // viewer.scene.camera.flyTo({
      //   destination: new Cesium.Cartesian3(3827270.552916987, -255123.18143177085, 5079147.091351856),
      //   orientation: new Cesium.HeadingPitchRoll(3.2624281242239963, -0.22213535190506972, 6.282786783842843),
      //   duration: 5.0
      // });
    },

    onViewerInited(viewer) {
      let vm = this;
      new MousePosition(viewer, pos => {
        vm.nowPostion = pos;
      });
    },

    initViewer() {
      let vm = this;
      Cesium.Ion.defaultAccessToken = $accessToken.cs;

      let osm = new Cesium.OpenStreetMapImageryProvider({
        url: 'https://a.tile.openstreetmap.org/'
      });
      let viewer = new Cesium.Viewer('cesium-drawer', {
        // imageryProvider: osm,
        terrainProvider: Cesium.createWorldTerrain(),
        contextOptions: {
          webgl: {
            alpha: true
          }
        },
        selectionIndicator: false,
        // shadows: true,
        // terrainShadows: Cesium.ShadowMode.ENABLED,
        animation: false, //是否显示动画控件
        baseLayerPicker: false, //是否显示图层选择控件
        geocoder: false, //是否显示地名查找控件
        // timeline: false, //是否显示时间线控件
        sceneModePicker: false, //是否显示投影方式控件
        navigationHelpButton: false, //是否显示帮助信息控件
        infoBox: false, //是否显示点击要素之后显示的信息
        fullscreenButton: false,
        shouldAnimate: true
      });

      // viewer.scene.globe.depthTestAgainstTerrain = true;

      this.viewer = viewer;
      this.onViewerInited(viewer);

      this.initEffects();

      // this.viewer.flyTo(this.viewer.entities);
    },

    onClick(value) {
      switch (value) {
        case 'play':
          this.stratPlay();
          break;
        case 'stop':
          this.stopPlay();
          break;
      }
    },

    stratPlay() {
      const viewer = this.viewer;
      if (((viewer.clock.shouldAnimate = !0), this.stopTime)) {
        viewer.clock.currentTime = this.stopTime;
      } else {
        // var e = '2019-06-18';
        const t = new Date(),
          i = '00',
          a = '24',
          r = new Date(new Date(t).setHours(Number(i))),
          o = new Date(new Date(t).setHours(Number(a)));
        (viewer.scene.globe.enableLighting = !0),
          (viewer.shadows = !0),
          (viewer.clock.startTime = Cesium.JulianDate.fromDate(r)),
          (viewer.clock.currentTime = Cesium.JulianDate.fromDate(r)),
          (viewer.clock.stopTime = Cesium.JulianDate.fromDate(o)),
          (viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP),
          (viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER),
          (viewer.clock.multiplier = 1600);
      }
    },

    stopPlay() {
      const viewer = this.viewer;
      (this.stopTime = viewer.clock.currentTime), (viewer.clock.shouldAnimate = !1);
    }
  }
});
</script>

<style lang="less">
.cesium-draw {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: white;
  font-size: 14px;

  #cesium-drawer {
    width: 100%;
    height: 100%;
    overflow: hidden;
    .cesium-viewer-bottom {
      display: none !important;
    }
  }

  .mouse-postion {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.8;
  }
}

.chart-container {
  position: absolute;
  bottom: 7px;
  left: 7px;
  background-color: rgba(0, 0, 0, 0.5);

  #profileChart {
    width: 600px;
    height: 300px;
  }
}
#canvas-a {
  top: 10px;
  display: none;
}

#canvas-b {
  top: 120px;
  display: none;
}

.tool-box {
  position: absolute;
  top: 70px;
  left: 10px;
  z-index: 9;

  #menu {
    padding-top: 20px;
    button {
      background-color: rgba(0, 0, 0, 0.75);
      border-color: rgba(0, 0, 0, 0);
      cursor: pointer;
      transition: all 500ms;
      &:hover {
        opacity: 0.75;
      }
    }
  }

  .slider-box-wrapper {
    width: 18em;
    padding: 0 10px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
  }

  .select-box-wrapper {
    padding: 5px 15px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
    width: auto;
  }
}
</style>
